/* 
 * File:   ResourceManager.cpp
 * Author: aljosa
 * 
 * Created on June 17, 2010, 3:35 PM
 */

#include "ResourceManager.h"
#include "messages.h"


namespace xEngine
{
    namespace Resources
    {
        // Only instance
        ResourceManager ResourceManager::instance;


        //
        // Constructors, destructors
        //
        ResourceManager::ResourceManager() {
        }

        ResourceManager::~ResourceManager() {
        }

        bool ResourceManager::init() {
            // TODO

            // Inform logging system
            Messages::initMessage("Resource manager", true);

            return true;
        }

        void ResourceManager::shutDown() {
            Messages::infoMessage("Resource manager shutdown ...");
        }

        // Resource setters, getters

        // If texture exists, return's it's addres. Otherwise loads it
        Texture* ResourceManager::getTexture(string filename)  {

            map<string, Texture>::iterator it; // the iterator
            it = this->textures.find(filename);

            // Is resource with given path already in memory?
            if (it!=this->textures.end())
                 return &((*it).second);


            // Otherwise, load it
            Texture tex( filename );

            // At the moment, only TGA is supported
            // TODO: support more
            tex.loadTGA();
            tex.makeTgaTexture( true );

            it = this->textures.find(filename); // Not very optimal; fix

            // add to hash map
            textures.insert ( pair<string,Texture>( filename, tex ) );
            return &((*it).second);
        }

        
        //
        // Singleton
        //
        ResourceManager &ResourceManager::getInstance() {
            return instance;
        }
    }
}

